class COLLITIONSCALASSIST extends eui.Component implements  eui.UIComponent {

	button1:eui.Button
	button2:eui.Button
	button3:eui.Button
	button4:eui.Button
	button5:eui.Button
	button6:eui.Button
	button7:eui.Button
	button8:eui.Button
	fistpos:egret.Point
	mode:string
	public constructor() {
		super();
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{
		//console.log("111111111111111111111111111111111111111") 必须添加场景才会调用
		super.childrenCreated();

		this.button1.visible=false
		this.button2.visible=false
		this.button3.visible=false
		this.button4.visible=false
		this.button5.visible=false
		this.button6.visible=false
		this.button7.visible=false
		this.button8.visible=false

		this.button1.name="leftup"
		this.button2.name="up"
		this.button3.name="rightup"
		this.button4.name="right"
		this.button5.name="rightdown"
		this.button6.name="down"
		this.button7.name="leftdown"
		this.button8.name="left"
	}
	private rectscal(e:egret.TouchEvent)
	{
		  if(e.type==egret.TouchEvent.TOUCH_END)
	       {
		      
		        egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.rectscal,this);
		        egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.rectscal,this);
		        return;
	       }
           else if(e.type==egret.TouchEvent.TOUCH_BEGIN&&e.target.name=="")return;
		   else 
		   {
			   if(e.type==egret.TouchEvent.TOUCH_BEGIN)
			   {
                    this.fistpos=new egret.Point(e.stageX,e.stageY);
				  	this.mode=e.target.name
		            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.rectscal,this);
		            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.rectscal,this);
					return;
			   }

		   }
		let nowpos=new egret.Point(e.stageX,e.stageY)
		let target=CCollitionEditor.editor.goal
		if(this.mode=="leftup")
	   {
         let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		 console.log(vector)

		
		 let width=target.shapepoint[1].x-target.shapepoint[0].x+vector.x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y+vector.y

		 target.setshape(x,y,width,height)

         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }
	   else if(this.mode=="up")
	   {
         let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		  let width=target.shapepoint[1].x-target.shapepoint[0].x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y+vector.y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()
	   }
	    else if(this.mode=="rightup")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		 let width=target.shapepoint[1].x-target.shapepoint[0].x-vector.x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y+vector.y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }
	    else if(this.mode=="left")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		  let width=target.shapepoint[1].x-target.shapepoint[0].x+vector.x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }
	    else if(this.mode=="leftdown")
	   {
		let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		  let width=target.shapepoint[1].x-target.shapepoint[0].x+vector.x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y-vector.y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }

	    else if(this.mode=="down")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		  let width=target.shapepoint[1].x-target.shapepoint[0].x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y-vector.y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }
	    else if(this.mode=="rightdown")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		 let width=target.shapepoint[1].x-target.shapepoint[0].x-vector.x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y-vector.y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }
	    else if(this.mode=="right")
	   {
		let vector=this.fistpos.subtract(nowpos)
		 let x=target.shape.x
		 let y=target.shape.y
		  let width=target.shapepoint[1].x-target.shapepoint[0].x-vector.x
		 let height=target.shapepoint[2].y-target.shapepoint[1].y
		 target.setshape(x,y,width,height)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)
		 this.rectassist()

	   }
	 
	}
	private circlescal(e:egret.TouchEvent)
	{
         if(e.type==egret.TouchEvent.TOUCH_END)
	       {
		      
		        egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.circlescal,this);
		        egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.circlescal,this);
		        return;
	       }
           else if(e.type==egret.TouchEvent.TOUCH_BEGIN&&e.target.name=="")return;
		   else 
		   {
			   if(e.type==egret.TouchEvent.TOUCH_BEGIN)
			   {
                    this.fistpos=new egret.Point(e.stageX,e.stageY);
				  	this.mode=e.target.name
		            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.circlescal,this);
		            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.circlescal,this);
					return;
			   }

		   }
		let nowpos=new egret.Point(e.stageX,e.stageY)
		let target=CCollitionEditor.editor.goal
		if(this.mode=="leftup")
	   {
         let vector=this.fistpos.subtract(nowpos)
		 let addr=Math.sqrt(vector.x*vector.x+vector.y*vector.y)
		 if(vector.x<0||vector.y<0)addr*=-1
		 let x=target.shape.x
		 let y=target.shape.y
	     let r=target.r+addr
		 target.setshape(x,y,r)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.circlescal,this)
		 this.circleassist()

	   }
	  
	    else if(this.mode=="rightup")
	   {
		  let vector=this.fistpos.subtract(nowpos)
		 let addr=Math.sqrt(vector.x*vector.x+vector.y*vector.y)
		 if(vector.x>0||vector.y<0)addr*=-1
		 let x=target.shape.x
		 let y=target.shape.y
	     let r=target.r+addr
		 target.setshape(x,y,r)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.circlescal,this)
		 this.circleassist()

	   }
	 
	    else if(this.mode=="leftdown")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 let addr=Math.sqrt(vector.x*vector.x+vector.y*vector.y)
		 if(vector.x<0||vector.y>0)addr*=-1
		 let x=target.shape.x
		 let y=target.shape.y
	     let r=target.r+addr
		 target.setshape(x,y,r)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.circlescal,this)
		 this.circleassist()

	   }

	    else if(this.mode=="rightdown")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 let addr=Math.sqrt(vector.x*vector.x+vector.y*vector.y)
		 if(vector.x>0||vector.y>0)addr*=-1
		 let x=target.shape.x
		 let y=target.shape.y
	     let r=target.r+addr
		 target.setshape(x,y,r)
         this.fistpos=nowpos
		 this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.circlescal,this)
		 this.circleassist()

	   }
	
	 
	}
	private polyscal(e:egret.TouchEvent)
	{
		  if(e.type==egret.TouchEvent.TOUCH_END)
	       {
		      
		        egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.polyscal,this);
		        egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.polyscal,this);
		        return;
	       }
           else if(e.type==egret.TouchEvent.TOUCH_BEGIN&&e.target.name=="")return;
		   else 
		   {
			   if(e.type==egret.TouchEvent.TOUCH_BEGIN)
			   {
                    this.fistpos=new egret.Point(e.stageX,e.stageY);
				  	this.mode=e.target.name
		            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.polyscal,this);
		            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.polyscal,this);
					return;
			   }

		   }
	
		let target=CCollitionEditor.editor.goal
		let stagexy=target.parent.globalToLocal(e.stageX,e.stageY)
		if(this.mode=="leftup")
		{
          target.shapepoint[0]=stagexy
		  CCollitionEditor.editor.changegoalshape();
          this.button1.x=e.stageX
		  this.button1.y=e.stageY

		}
		else if(this.mode=="up")
		{
          target.shapepoint[1]=stagexy
		  CCollitionEditor.editor.changegoalshape();
		   this.button2.x=e.stageX
		  this.button2.y=e.stageY

		}
		else if(this.mode=="rightup")
		{
          target.shapepoint[2]=stagexy
		  CCollitionEditor.editor.changegoalshape();
		   this.button3.x=e.stageX
		  this.button3.y=e.stageY

		}
		else if(this.mode=="right")
		{
          target.shapepoint[3]=stagexy
		  CCollitionEditor.editor.changegoalshape();
		   this.button4.x=e.stageX
		  this.button4.y=e.stageY

		}
		else if(this.mode=="rightdown")
		{
          target.shapepoint[4]=stagexy
		  CCollitionEditor.editor.changegoalshape();
		   this.button5.x=e.stageX
		  this.button5.y=e.stageY

		}
	}
	public rectassist()
	{
		this.resetbutton()
       let target=CCollitionEditor.editor.goal
	   let stagepos=target.parent.localToGlobal(target.shapepoint[0].x,target.shapepoint[0].y)
	   this.button1.x=stagepos.x
	   this.button1.y=stagepos.y
       this.button2.x=this.button1.x+target.shape.width/2-this.button2.width/2
	   this.button2.y=this.button1.y
	   stagepos=target.parent.localToGlobal(target.shapepoint[1].x,target.shapepoint[1].y)
	   this.button3.x=stagepos.x
	   this.button3.y=stagepos.y
       this.button4.x=this.button3.x
	   this.button4.y=this.button3.y+target.shape.height/2-this.button4.height/2
	    stagepos=target.parent.localToGlobal(target.shapepoint[2].x,target.shapepoint[2].y)
	   this.button5.x=stagepos.x
	   this.button5.y=stagepos.y
       this.button6.x= this.button2.x
	   this.button6.y=this.button5.y
	    stagepos=target.parent.localToGlobal(target.shapepoint[3].x,target.shapepoint[3].y)
	   this.button7.x=stagepos.x
	   this.button7.y=stagepos.y
       this.button6.x=this.button2.x
	   this.button8.x=this.button1.x
	   this.button8.y=this.button4.y

	   this.button1.visible=true
	   this.button2.visible=true
	   this.button3.visible=true
	   this.button4.visible=true
	   this.button5.visible=true
	   this.button6.visible=true
	   this.button7.visible=true
	   this.button8.visible=true

	   this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)

	}
	public circleassist()
	{
		this.resetbutton()
	   let target=CCollitionEditor.editor.goal
	   let stagepos=target.parent.localToGlobal(target.shapepoint[0].x,target.shapepoint[0].y)
	  console.log(this.button1)
      this.button1.x=stagepos.x-target.r
	  this.button1.y=stagepos.y-target.r
	  this.button3.x=stagepos.x+target.r
	  this.button3.y=this.button1.y

	  this.button5.x= this.button3.x
	  this.button5.y=stagepos.y+target.r

	  this.button7.x=this.button1.x
	  this.button7.y= this.button5.y
	  this.button1.visible=true
	  this.button3.visible=true
	  this.button5.visible=true
	  this.button7.visible=true
	  this.button2.visible=false
	  this.button4.visible=false
	  this.button6.visible=false
	  this.button8.visible=false
	    this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.circlescal,this)
	}
	public threepointassist()
	{this.resetbutton()
	   let target=CCollitionEditor.editor.goal
	   let stagepos=target.parent.localToGlobal(target.shapepoint[0].x,target.shapepoint[0].y)
	   this.button1.x=stagepos.x
	   this.button1.y=stagepos.y
	    stagepos=target.parent.localToGlobal(target.shapepoint[1].x,target.shapepoint[1].y)
       this.button2.x=stagepos.x
	   this.button2.y=stagepos.y
	   stagepos=target.parent.localToGlobal(target.shapepoint[2].x,target.shapepoint[2].y)
	   this.button3.x=stagepos.x
	   this.button3.y=stagepos.y

	   this.button1.visible=true
	   this.button2.visible=true
	   this.button3.visible=true
	   this.button4.visible=false
		this.button5.visible=false
		this.button6.visible=false
		this.button7.visible=false
		this.button8.visible=false

        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.polyscal,this)
	  
	}
	public fourpointassist()
	{this.resetbutton()
	  let target=CCollitionEditor.editor.goal
	   let stagepos=target.parent.localToGlobal(target.shapepoint[0].x,target.shapepoint[0].y)
	   this.button1.x=stagepos.x
	   this.button1.y=stagepos.y
	    stagepos=target.parent.localToGlobal(target.shapepoint[1].x,target.shapepoint[1].y)
       this.button2.x=stagepos.x
	   this.button2.y=stagepos.y
	   stagepos=target.parent.localToGlobal(target.shapepoint[2].x,target.shapepoint[2].y)
	   this.button3.x=stagepos.x
	   this.button3.y=stagepos.y
	   stagepos=target.parent.localToGlobal(target.shapepoint[3].x,target.shapepoint[3].y)
       this.button4.x=stagepos.x
	   this.button4.y=stagepos.y


	   this.button1.visible=true
	   this.button2.visible=true
	   this.button3.visible=true
	   this.button4.visible=true
	   this.button5.visible=false
		this.button6.visible=false
		this.button7.visible=false
		this.button8.visible=false
	  this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.polyscal,this)
	}

	public fivepointassist()
	{
		this.resetbutton()
	  	let target=CCollitionEditor.editor.goal
	   let stagepos=target.parent.localToGlobal(target.shapepoint[0].x,target.shapepoint[0].y)
	   this.button1.x=stagepos.x
	   this.button1.y=stagepos.y
	    stagepos=target.parent.localToGlobal(target.shapepoint[1].x,target.shapepoint[1].y)
       this.button2.x=stagepos.x
	   this.button2.y=stagepos.y
	   stagepos=target.parent.localToGlobal(target.shapepoint[2].x,target.shapepoint[2].y)
	   this.button3.x=stagepos.x
	   this.button3.y=stagepos.y
	   stagepos=target.parent.localToGlobal(target.shapepoint[3].x,target.shapepoint[3].y)
       this.button4.x=stagepos.x
	   this.button4.y=stagepos.y
        stagepos=target.parent.localToGlobal(target.shapepoint[4].x,target.shapepoint[4].y)
       this.button5.x=stagepos.x
	   this.button5.y=stagepos.y

	   this.button1.visible=true
	   this.button2.visible=true
	   this.button3.visible=true
	   this.button4.visible=true
	   this.button5.visible=true
       this.button6.visible=false
	   this.button7.visible=false
	   this.button8.visible=false
	   this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.polyscal,this)
	 
	}
	public resetbutton()
	{
		console.log(this.button1)
		this.button1.visible=false
		this.button2.visible=false
		this.button3.visible=false
		this.button4.visible=false
		this.button5.visible=false
		this.button6.visible=false
		this.button7.visible=false
		this.button8.visible=false

	    this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.polyscal,this)
	
	    this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.circlescal,this)
	
	    this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rectscal,this)

	}
	
}